//
//  UIColor+MY.m
//  MiaoYin
//
//  Created by juweitu on 2020/3/3.
//  Copyright © 2020 thy. All rights reserved.
//

#import "UIColor+MY.h"

/** 十六进制字符串转数字 */
NSUInteger MX_HexStrToInt(NSString *str) {
    uint32_t result = 0;
    sscanf([str UTF8String], "%X", &result);
    return result;
}

/** 提取十六进制字符串中的色值 */
BOOL MX_HexStrToRGBA(NSString *str,
                     NSInteger *r, NSInteger *g, NSInteger *b, NSInteger *a) {
    str = [str uppercaseString];
    if ([str hasPrefix:@"#"]) {
        str = [str substringFromIndex:1];
    } else if ([str hasPrefix:@"0X"]) {
        str = [str substringFromIndex:2];
    }
    NSUInteger length = [str length];
    //         RGB            RGBA          RRGGBB        RRGGBBAA
    if (length != 3 && length != 4 && length != 6 && length != 8) {
        return NO;
    }
    //RGB,RGBA,RRGGBB,RRGGBBAA
    if (length < 5) {
        *r = MX_HexStrToInt([str substringWithRange:NSMakeRange(0, 1)]);
        *g = MX_HexStrToInt([str substringWithRange:NSMakeRange(1, 1)]);
        *b = MX_HexStrToInt([str substringWithRange:NSMakeRange(2, 1)]);
        if (length == 4){
            *a = MX_HexStrToInt([str substringWithRange:NSMakeRange(3, 1)]);
        } else {
            *a = 255;
        }
    } else {
        *r = MX_HexStrToInt([str substringWithRange:NSMakeRange(0, 2)]);
        *g = MX_HexStrToInt([str substringWithRange:NSMakeRange(2, 2)]);
        *b = MX_HexStrToInt([str substringWithRange:NSMakeRange(4, 2)]);
        if (length == 8) {
            *a = MX_HexStrToInt([str substringWithRange:NSMakeRange(6, 2)]);
        } else {
            *a = 255;
        }
    }
    return YES;
}

@implementation UIColor (MY)

+ (UIColor *)colorWithUInt:(NSUInteger)rgb
{
    return [self colorWithUInt:rgb alpha:1];
}

+ (UIColor *)colorWithUInt:(NSUInteger)rgb alpha:(CGFloat)alpha
{
    //红色
    CGFloat r = (rgb >> 16)       / 255.0;
    ///绿色
    CGFloat g = (rgb >> 8 & 0xff) / 255.0;
    //蓝色
    CGFloat b = (rgb & 0xff)      / 255.0;
    
    return [UIColor colorWithRed:r green:g blue:b alpha:alpha];
}

+ (UIColor *)colorWithString:(NSString *)hexString alpha:(CGFloat)alpha{
    
    hexString = [[hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    UIColor *defaultColor = [UIColor clearColor];
    
    if (hexString.length < 6) return defaultColor;
    if ([hexString hasPrefix:@"#"]) hexString = [hexString substringFromIndex:1];
    if ([hexString hasPrefix:@"0X"] || [hexString hasPrefix:@"0x"]) hexString = [hexString substringFromIndex:2];
    if (hexString.length != 6) return defaultColor;
    
    //method1
    NSScanner *scanner = [NSScanner scannerWithString:hexString];
    unsigned int hexNumber;
    if (![scanner scanHexInt:&hexNumber]) return defaultColor;
    
    //method2
    const char *char_str = [hexString cStringUsingEncoding:NSASCIIStringEncoding];
    int hexNum;
    sscanf(char_str, "%x", &hexNum);
    
    return [UIColor colorWithUInt:hexNumber alpha:alpha];
}

+ (UIColor *)colorWithString:(NSString *)hexString{
    return [UIColor colorWithString:hexString alpha:1.0f];
}

+ (instancetype)mx_transitionColorWithStartColor:(UIColor *)startColor endColor:(UIColor *)endColor andCoe:(double)coe {
    // 获取开始位置颜色的 R、G、B、A值
    CGFloat startR, startG, startB, startA;
    if (![startColor getRed:&startR green: &startG blue:&startB alpha:&startA]) {
        return nil;
    }
    // 获取结束位置颜色的 R、G、B、A值
    CGFloat endR, endG, endB, endA;
    if (![endColor getRed:&endR green: &endG blue:&endB alpha:&endA]) {
        return nil;
    }
    return [self mx_transitionColorWithStartValue:MX_ColorValueMake(startR * 255, startG * 255, startB * 255, startA * 255)
                                      endValue:MX_ColorValueMake(endR * 255, endG * 255, endB * 255, endA * 255)
                                        andCoe:coe];
}

+ (instancetype)mx_transitionColorWithStartHex:(NSString *)startHex endHex:(NSString *)endColorHex andCoe:(double)coe {
    // 获取开始位置颜色的 R、G、B、A值
    NSInteger startR, startG, startB, startA;
    if (!MX_HexStrToRGBA(startHex, &startR, &startG, &startB, &startA)) {
        return nil;
    }
    // 获取结束位置颜色的 R、G、B、A值
    NSInteger endR, endG, endB, endA;
    if (!MX_HexStrToRGBA(endColorHex, &endR, &endG, &endB, &endA)) {
        return nil;
    }
    return [self mx_transitionColorWithStartValue:MX_ColorValueMake(startR, startG, startB, startA)
                                      endValue:MX_ColorValueMake(endR, endG, endB, endA)
                                        andCoe:coe];
}

+ (instancetype)mx_transitionColorWithStartValue:(MX_ColorValue)startValue endValue:(MX_ColorValue)endValue andCoe:(double)coe {
    // 开始颜色与结束的色值差
    NSInteger R_D_value = endValue.r - startValue.r;
    NSInteger G_D_value = endValue.g - startValue.g;
    NSInteger B_D_value = endValue.b - startValue.b;
    NSInteger A_D_value = endValue.a - startValue.a;
    // (开始值 + 差值*coe)/255.0 = 最终色值
    CGFloat R = (startValue.r + (R_D_value * coe))/255.0;
    CGFloat G = (startValue.g + (G_D_value * coe))/255.0;
    CGFloat B = (startValue.b + (B_D_value * coe))/255.0;
    CGFloat A = (startValue.a + (A_D_value * coe))/255.0;
    return [UIColor colorWithRed:R green:G blue:B alpha:A];
}

@end
